# -*- coding: utf-8 -*-

'''

@author: wyndem
@Emil:   wyndem.wen@timevary.com
@FileName: demo.py
@Time: 2018/7/19 14:13
@Description: 

'''
import asyncio
import datetime
import json
import time

import requests as r
import re
import config
import lol
from lol import coinsuper
from mail import Mail163
import tool
from tool import TOOL as t

cookie={}


async def denglu():
    m = Mail163('coinsuper', 'Welcome to', True)
    user = 'vhp707775881@163.com'
    pwd = 'h7058748'
    c = coinsuper()
    v = c.getjiyan_key()
    geetest_key = c.geetest_key(v, user)
    code=c.login_mail(user,pwd,10)
    m=c.actregister(user,code,geetest_key)
    print(m)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(denglu())

if __name__ != '__main__':
    user='ev727njxw216@163.com'
    pwd='ulgb61crd811'
    c=coinsuper()
    v=c.getjiyan_key()
    geetest_key=c.geetest_key(v,user)
    c.send_sms(user,geetest_key)
    code=c.login_mail(user,pwd,25)
    c.actregister(user,code,geetest_key)

if __name__ != '__main__':


    m=Mail163('coinsuper ','Welcome to',True)
    coinsuper=coinsuper()

    proxies = {
        "https": t.getProXY(),
    }

    jy=r.get('https://share.coinsuper.shop/v1/common/gt/startCaptcha?clientType=WEB',headers=t.getHeaders(),proxies=proxies)
    cookie=jy.cookies
    data=jy.json().get('data')
    challenge=data.get('challenge')
    gt=data.get('gt')

    #获取验证码
    jypamr={
        'gt':str(gt),
        'challenge':str(challenge),
        'url':'https://share.coinsuper.shop/',
    }
    v=t.jiyanapi_s(True,jypamr)

    # 进入循环
    f = open('exx.txt', 'r', encoding='utf-8')
    for line in f.readlines():
        l = line.strip().replace('\n', '')
        if l == '':
            continue
        l = re.split('-+', l)

        #获取geetest_code_key
        json_data={
            "lang":"en-GB",
            "source":"WEB",
            "geetestChallenge":v[0],
            "geetestValidate":v[1],
            "geetestSeccode":v[1]+"|jordan",
            "clientType":"WEB",
            "userEmail":l[0],
            "operationType":"2"
        }
        header =t.getHeaders()
        header['Content - Type'] = "application/json;charset=UTF-8"
        header['X-B3-Traceid'] = "9084308976537210"
        header['XX-B3-Spanid'] = "9084308976537210"
        header['Host'] = "www.coinsuper.com"
        header['X-Requested-With'] = "XMLHttpRequest"
        header['Referer'] = "https://www.coinsuper.com/"
        header['Accept-Language'] = "zh-CN,zh;q=0.8"
        header['Accept'] = "*/*"
        geetest_code_key=r.post('https://share.coinsuper.shop/v1/common/gt/validate',proxies=proxies,data=json.dumps(json_data),headers=header).json()
        geetest_code_key=geetest_code_key .get('data').get('geetest_code_key')


        #发送验证码
        s_time= int(time.time() * 1000)
        sign = t.md5Pass('accesskey=webAccessKey&email=%s&emailAuthType=RG&lang=en-GB&time=%s' % (l[0], str(s_time)))
        json_data={
            "common":{
                    "time":str(s_time),
                    "lang":"en-GB",
                    "sign": sign
            },
            "data":{
                "source":"WEB",
                "email":l[0],
                "emailAuthType":"RG",
                "geetestKey":geetest_code_key
            }
        }
        res=r.post('https://share.coinsuper.shop/v1/member/send/remail',data=json.dumps(json_data),headers=header,proxies=proxies).json()
        #当前发送验证码的时间
        s_time=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
        print(res)
        print(s_time)

        mail_code=0
        #登录邮箱
        if  m.login(l[0],l[1]):
           for i in range(25):
               print('尝试第%d次'%(i+1))
               time.sleep(i + 1)
               mail_code=m.integrate_mail(s_time)
               print(mail_code)
               if mail_code!=None:
                   break
        else:
            print('登录失败')

        mail_code=coinsuper.getvrif(mail_code[1]," code is")
        print('验证码是：%s'%(mail_code))

        #注册
        json_data={
            "lang":"en-GB",
            "data":
                {
                    "source":"WEB",
                    "email":l[0],
                    "loginPwd":t.md5Pass(t.create_pwd()),
                    "emailCode":mail_code,
                    "geetestKey":geetest_code_key,
                    "activityCode":"PRL_AIRDROP"
                }
        }
        res=r.post('https://www.coinsuper.com/v1/member/user/actregister',data=json.dumps(json_data),headers=header,proxies=proxies).json()
        print(res)
